<?php 


/**
 * 接收普通消息
 */
abstract class WechatReceiveMsg
{
    public $toUserName;   // 开发者微信号
    public $fromUserName; // 发送方帐号（一个OpenID）
    public $createTime;   // 消息创建时间 （整型）
    public $msgType;      // 文本类型
    public $msgId;        // 消息id，64位整型
    public $msgxml;       // 便于子类初始化特有属性
    
    /**
     * 初始化收到的消息
     */
    public function __construct($msgxml)
    {
        $this->toUserName = $msgxml->ToUserName;
        $this->fromUserName = $msgxml->FromUserName;
        $this->createTime = $msgxml->CreateTime;
        $this->msgType = $msgxml->MsgType;
        $this->msgId = $msgxml->MsgId;
        $this->msgxml = $msgxml;
    }
    
    /**
     * 构建消息体
     */
    abstract public function makeContent();
}


/**
 * 接受的文本消息
 */
class WechatRTextMsg extends WechatReceiveMsg
{
    public $content = '';
    
    /**
     * 初始化文本消息
     */
    public function __construct($dataStr)
    {
        parent::__construct($dataStr);
        $this->makeContent();
    }
    
    /**
     * 构建消息体
     */
    public function makeContent()
    {
        $this->content = $this->msgxml->Content;
    }
}  


/**
 * 图片消息
 */
class WechatRImageMsg extends WechatReceiveMsg
{
    public $picUrl;
    public $mediaId;
    
    /**
     * 初始化文本消息
     */
    public function __construct($dataStr)
    {
        parent::__construct($dataStr);
        $this->makeContent();
    }
    
    /**
     * 构建消息体
     */
    public function makeContent()
    {
        $this->picUrl = $this->msgxml->PicUrl;
        $this->mediaId = $this->msgxml->MediaId;
    }
}


/**
 * 语言消息
 */
class WechatRVoiceMsg extends WechatReceiveMsg
{
    public $mediaId;
    public $format;
    
    /**
     * 初始化文本消息
     */
    public function __construct($dataStr)
    {
        parent::__construct($dataStr);
        $this->makeContent();
    }
    
    /**
     * 构建消息体
     */
    public function makeContent()
    {
        $this->mediaId = $this->msgxml->MediaId;
        $this->format  = $this->msgxml->Format;
    }
}


/**
 * 视频消息
 */
class WechatRVideoMsg extends WechatReceiveMsg
{
    public $mediaId;
    public $thumbMediaId;
    
    /**
     * 初始化文本消息
     */
    public function __construct($dataStr)
    {
        parent::__construct($dataStr);
        $this->makeContent();
    }
    
    /**
     * 构建消息体
     */
    public function makeContent()
    {
        $this->mediaId = $this->msgxml->MediaId;
        $this->thumbMediaId = $this->msgxml->ThumbMediaId;
    }
}